#!/bin/bash

log_file=/tmp/maxwell-upstart.log
DATE=`date`

if [ -s /etc/ros/setup.bash ]; then
    echo "$DATE: maxwell-start using setup file /etc/ros/setup.bash" >> $log_file
    source /etc/ros/setup.bash
else
    echo "$DATE: maxwell-start using setup file /opt/ros/electric/setup.bash" >> $log_file
    source /opt/ros/electric/setup.bash
fi

interface=wlan0
echo "$DATE: maxwell-start on interface $interface" >> $log_file

export ROS_IP=`ifconfig $interface | grep -o 'inet addr:[^ ]*' | cut -d: -f2`

echo "$DATE: maxwell-start setting ROS_IP=$ROS_IP" >> $log_file

if [ "$ROS_IP" = "" ]; then
    echo "$DATE: maxwell-start can't run with empty ROS_IP." >> $log_file
    exit 1
fi

if [ -s /etc/ros/maxwell/bringup.launch ]; then
    echo "Using /etc/ros/maxwell/bringup.launch" >> $log_file
    setuidgid maxwell roslaunch /etc/ros/maxwell/bringup.launch
else
    setuidgid maxwell roslaunch maxwell_defs bringup.launch
fi
